package cn.enilu.flash.mobile.controller;

import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.entity.release.TaskInfo;
import cn.enilu.flash.bean.entity.shop.*;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.service.release.TaskInfoService;
import cn.enilu.flash.service.shop.CategoryService;
import cn.enilu.flash.utils.Lists;
import cn.enilu.flash.utils.StringUtil;
import cn.enilu.flash.utils.factory.Page;
import cn.enilu.flash.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/task_info")
public class TaskInfoController extends BaseController {

    @Autowired
    private TaskInfoService taskInfoService;
    
    @Autowired
    private CategoryService categoryService;
    /**
     * 获取指定类别下的任务发布列表
     *
     * @param idCategory
     * @return
     */
    @GetMapping("/query_task_info")
    public Object queryTaskInfo(@RequestParam("idCategory") Long idCategory) {
        Page<TaskInfo> page = new PageFactory<TaskInfo>().defaultPage();
        List<Category> categories = categoryService.queryAll(SearchFilter.build("pid",idCategory));
        List<Long> ids = Lists.newArrayList(idCategory);
        categories.forEach(item->{
            ids.add(item.getId());
        });
        if(ids.size()==1) {
            page.addFilter(SearchFilter.build("idCategory", SearchFilter.Operator.EQ, idCategory));
        }else{
            page.addFilter(SearchFilter.build("idCategory", SearchFilter.Operator.IN, ids));
        }
        page.addFilter(SearchFilter.build("isOnSale", true));
        page = taskInfoService.queryPage(page);
        return Rets.success(page);
    }

    /**
     * 根据关键字搜索发布任务
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public Object search(@RequestParam("key") String key) {
        Page<TaskInfo> page = new PageFactory<TaskInfo>().defaultPage();
        if (StringUtil.isNotEmpty(key)) {
            page.addFilter(SearchFilter.build("name", SearchFilter.Operator.LIKE, key));
        }
        page.addFilter(SearchFilter.build("isOnSale", true));
        page = taskInfoService.queryPage(page);
        return Rets.success(page);
    }

    @RequestMapping(value = "/searchNew", method = RequestMethod.GET)
    public Object searchNew() {
        List<TaskInfo> list = taskInfoService.queryAll(Lists.newArrayList(
                SearchFilter.build("isNew", true),
                SearchFilter.build("isOnSale", true)
        ));
        return Rets.success(list);
    }

    /**
     * 查询热门商品
     *
     * @return
     */
    @RequestMapping(value = "/searchHot", method = RequestMethod.GET)
    public Object searchHot() {
        List<TaskInfo> list = taskInfoService.queryAll(Lists.newArrayList(
                SearchFilter.build("isHot", true),
                SearchFilter.build("isOnSale", true)
        ));
        return Rets.success(list);
    }

    /**
     * 根据ID查询详细内容
     * @param id
     * @return
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Object get(@PathVariable Long id) {
        TaskInfo taskInfo = taskInfoService.get(id);



        return Rets.success(taskInfo);
    }
}
